package yxy.game.pm2.command.bandit;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.BanditMessage;
import yxy.game.pm2.module.bandit.BanditHandle;

import java.util.List;

/** 剿匪 剿匪任务派出 */
@MessageType(proto = Command.BanditTaskDeliver, classtype = BanditMessage.BanditTaskDeliverRequest.class)
public class CMD_BanditTaskDeliver extends Command<BanditMessage.BanditTaskDeliverRequest> {

	@Override
	public void execute(MessageSource messageSource, BanditMessage.BanditTaskDeliverRequest request) {
		String playerCode = messageSource.getTag();
		int taskId = request.getId();
		List<String> sending = request.getSendingList();

		BanditMessage.BanditTaskDeliverResponse.Builder response = BanditMessage.BanditTaskDeliverResponse.newBuilder();

		try {
			Result result = new BanditHandle().sendTask(playerCode, taskId, sending);
			send(messageSource, response, result);

			new BanditHandle().updateBanditTip(playerCode);
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		}
	}

	private void send(MessageSource messageSource, BanditMessage.BanditTaskDeliverResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}